/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package general.util;

import java.text.DecimalFormat;

/**
 *
 * @author isaias
 */
public class MyStringUtil {
     public static boolean isNullOrEmpty(String text) {
        return text == null ? true : (text.trim().isEmpty() ? true : false);
    }

    public static String capitalize(String string) {
        if (string == null) {
            return null;
        }

        if (string.length() == 0) {
            return string;
        }

        String[] temp = string.split("[ ]");

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < temp.length; i++) {
            String s = temp[i];
            if(i != 0){
                sb.append(" ");
            }
            if (s.length() > 2) {
                sb.append(s.substring(0, 1).toUpperCase());
                sb.append(s.substring(1).toLowerCase());
            } else {
                sb.append(s.toLowerCase());
            }

        }

        return sb.toString();
    }

    public static String toUpperCase(String text) {
        return text == null ? null : (text.toUpperCase());
    }

    public static String toUpdateFormat(String[] forUpdate, String preId) {
        String resp = "";

        for (String string : forUpdate) {
            resp += (preId + string) + " ";
        }

        if (isNullOrEmpty(resp)) {
            return "@none";
        }

        return resp;
    }

    public static String replaceString(String value){
        if(isNullOrEmpty(value)){
            return "--";
        }

        return value.toUpperCase();
    }

    public static String formatMonetary(double value){
        DecimalFormat df = new DecimalFormat("#,###.00");
        String s = df.format(value);
        s = s.replaceAll("\\.", "*");
        s = s.replaceAll(",", "\\.");
        s = s.replaceAll("\\*", ",");

        return s;
    }

    public static String replaceHtmlData(String description) {
        description = description.replaceAll("</div>", "\n");
        description = description.replaceAll("\\<.*?>", "");
        return description;
    }
}
